function clearForm() {
    var $ = layui.$;
    $("input[name='putbookName'] ").val("");
    $("input[name='putbookOrdnum'] ").val("");
    $("input[name='suppName'] ").val("");
    $("input[name='suppAddr'] ").val("");
    $("input[name='suppNum'] ").val("");
    $("input[name='putbookNum'] ").val("");
    $("input[name='areaName'] ").val("");
    $("input[name='workName']").val("");
}
(function () {
    findWork();
    var form = layui.form;
    form.verify({
        checkPutbookName: function (value) {
            if (value.length == 0) {
                return "图书名称不能为空";
            }
        },
        checkPutbookOrdnum: function (value) {
            if (value.length ==0) {
                return "入库单号不能为空";
            }
        },
        checkSuppName: function (value) {
            if (value.length ==0) {
                return "供货商不能为空";
            }
        },
        checkSuppAddr: function (value) {
            if (value.length ==0) {
                return "地址不能为空";
            }
        },
        checkSuppNum: function (value) {
            if (value.length ==0) {
                return "联系电话不能为空";
            }
        },
        checkSutbookNum: function (value) {
            if (value.length ==0) {
                return "数量不能为空";
            }
        },
        checkAreaName: function (value) {
            if (value.length ==0) {
                return "区域不能为空";
            }
        }
    });

    form.on('submit(addPutBookBtn)', function (data) {
        var $ = layui.$;
        var putbookName = data.field.putbookName;
        var putbookOrdnum =data.field.putbookOrdnum;
        var suppName =data.field.suppName;
        var suppAddr =data.field.suppAddr;
        var suppNum =data.field.suppNum;
        var putbookNum =data.field.putbookNum;
        var areaName =data.field.areaName;
        var workName =data.field.workName;
        $.ajax({
            url: "../PutBook/insert",
            method: "post",
            data: {
                "putbookName": putbookName,
                "putbookOrdnum": putbookOrdnum,
                "suppName": suppName,
                "suppAddr":suppAddr,
                "suppNum": suppNum,
                "putbookNum": putbookNum,
                "areaName": areaName,
                "workName": workName
            },
            success: function (data) {
                $.ajax({
                    url: "../Book/insert",
                    method: "post",
                    data: {
                        "putbookName": putbookName,
                        "suppName": suppName,
                        "putbookNum": putbookNum,
                        "areaName": areaName
                    },
                    success: function (data) {
                        clearForm();
                        if (data.code == 200) {
                            layui.table.reload("demo");
                        } else if (data.code == 500) {
                        }
                    },
                    error: function (data) {
                        console.log(data);
                    }
                });
                layui.layer.msg(data.msg,{icon:1});
                setTimeout(function () {

                },1000)
                if (data.code == 200) {
                    layui.table.reload("demo");
                    clearForm();

                } else if (data.code == 500) {
                    var layer = layui.layer;
                    layer.alert(data.msg, {icon: 2});
                }
            },
            error: function (data) {
                console.log(data);
            }
        });
        return false;
    });
})();

function findWork(){
    var $ =layui.$;
    $.ajax({
        url: "../work/findWork",
        method:"get",
        success:function (result) {
            $("#workName").empty();
            if (result.code == 200) {
                $("#workName").append(new Option("请选择操作人员", ""));
                $.each(result.data, function (index, item) {
                    $('#workName').append(new Option(item.workName, item.workName));
                });
            } else {
                $("#workName").append(new Option("暂无数据", ""));
            }
            layui.form.render("select");
        }
    });
}